import '../utils/tools.dart';

import 'comparable.dart';
import 'image_model.dart';
import 'serializable.dart';

class ArticleModel extends Serializable implements Equable<int> {
  int id = 0;
  String lang = '';
  int mainId = 0;
  int cateId = 0;
  int channelId = 0;

  String title = '';
  String viceTitle = '';
  String cover = '';
  String description = '';

  String categoryName = '';
  String categoryTitle = '';
  String channelName = '';
  String channelTitle = '';

  List<ImageModel>? images;
  Map<String, dynamic>? propData;

  String content = '';

  int createTime = 0;
  int updateTime = 0;

  int status = 0;
  int type = 0;
  int views = 0;
  int comment = 0;
  int digg = 0;

  ArticleModel.fromJson(Map? article) {
    if (article == null || !article.containsKey('id')) return;

    id = Tools.parseInt(article['id']);
    cateId = article['cate_id'] ?? 0;
    channelId = article['channel_id'] ?? 0;
    title = article['title'] ?? '';
    viceTitle = article['vice_title'] ?? '';
    cover = Tools.fixImageUrl(article['cover']);

    categoryName = article['category_name'] ?? '';
    categoryTitle = article['category_title'] ?? '';
    channelName = article['channel_name'] ?? '';
    channelTitle = article['channel_title'] ?? '';

    description = article['description'] ?? '';
    content = Tools.fixContentImageUrl(article['content']) ?? '';

    createTime = Tools.parseInt(article['create_time']);
    updateTime = Tools.parseInt(article['update_time']);

    status = Tools.parseInt(article['status']);
    type = Tools.parseInt(article['type']);
    views = Tools.parseInt(article['views']);
    comment = Tools.parseInt(article['comment']);
    digg = Tools.parseInt(article['digg']);

    if (article.containsKey('prop_data') && article['prop_data'] is Map) {
      propData = article['prop_data'];
    }

    List<dynamic>? images = article['images'];
    if (images != null && images.isNotEmpty) {
      this.images =
          images.map<ImageModel>((img) => ImageModel.fromJson(img)).toList();
    }
  }

  @override
  int get compareField => id;

  @override
  Map toJson() {
    return {
      'id': id,
      'cate_id': cateId,
      'channel_id': channelId,
      'title': title,
      'vice_title': viceTitle,
      'cover': cover,
      'description': description,
      'content': content,
      'create_time': createTime,
      'status': status,
      'type': type,
      'views': views,
      'prop_data': propData,
    };
  }
}
